package com.nervenets.shopping.service.impl;

import com.nervenets.general.enumeration.Action;
import com.nervenets.general.exception.LogicException;
import com.nervenets.general.service.impl.BaseServiceImpl;
import com.nervenets.general.web.params.Params;
import com.nervenets.shopping.controller.role.params.RoleGroupEditParams;
import com.nervenets.shopping.hibernate.dao.RoleGroupDao;
import com.nervenets.shopping.hibernate.domain.user.Manager;
import com.nervenets.shopping.hibernate.domain.user.RoleGroup;
import com.nervenets.shopping.service.ManagerService;
import com.nervenets.shopping.service.RoleGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleGroupServiceImpl extends BaseServiceImpl<RoleGroup, RoleGroupDao> implements RoleGroupService {
    @Resource
    private ManagerService managerService;

    @Override
    protected void before(Action action, RoleGroup roleGroup, Params... params) {
        super.before(action, roleGroup);
        switch (action) {
            case add:
                break;
            case update:
                checkInit(roleGroup);
                break;
            case delete:
                checkInit(roleGroup);
                long memberCount = managerService.countByRoleGroupsId(roleGroup);
                if (memberCount > 0) throw new LogicException("该权限组存在员工，不能删除");
                break;
        }
    }

    private void checkInit(RoleGroup roleGroup) {
        RoleGroup top1 = dao.findTop1ByOrderById();
        if (null != top1 && top1.getId().equals(roleGroup.getId())) {
            throw new LogicException("为保障安全，初始化权限组不允许修改，请重新创建权限组！");
        }
    }

    @Override
    protected void after(Action action, RoleGroup roleGroup, Params... params) {
        switch (action) {
            case add:
            case update:
                if (null != params) {
                    if (params.length > 0 && params[0] instanceof RoleGroupEditParams) {
                        RoleGroupEditParams roleGroupEditParams = (RoleGroupEditParams) params[0];
                        List<Manager> managers = managerService.findAllByRoleGroup(roleGroup);
                        for (Manager manager : managers) {
                            if (manager.getRoleGroups().contains(roleGroup)) {
                                manager.getRoleGroups().remove(roleGroup);
                                managerService.save(manager);
                            }
                        }

                        for (Long userId : roleGroupEditParams.getUserIds()) {
                            final Manager manager = managerService.findOne(userId, String.format("用户【%s】不存在", userId));
                            if (null != manager) {
                                if (!manager.getRoleGroups().contains(roleGroup)) {
                                    manager.getRoleGroups().add(roleGroup);
                                }
                                managerService.save(manager);
                            }
                        }
                    }
                }
                break;
            case delete:
                break;
        }
    }

    @Override
    public RoleGroup findFirst() {
        return dao.findTop1ByOrderById();
    }
}
